/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * @author yaoyuming
 * @date  2020-06-03 17:56
 */
package com.yao.common.filter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yao.common.helper.RequestHelper;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;

/**
 * @author yaoyuming
 * @date 2020/06/03 17:56
 */
public class RequestWrapper extends HttpServletRequestWrapper {


    /**
     * 年月日时分秒
     */
    private final static String YYYYMMDDHHMMSS= "yyyy-MM-dd HH:mm:ss";

    public RequestWrapper(HttpServletRequest request) {
        super(request);
        this.localDateTimeWrapper(request);
    }

    private void localDateTimeWrapper(HttpServletRequest request){
        String body = RequestHelper.getBody(request);

        Console.log("sb + {}", body);

        if(StrUtil.isNotEmpty(body)){
            JSONObject json = JSON.parseObject(body);
            json.keySet().stream().filter(k -> StrUtil.endWith(k, "Time")).forEach(e -> {
                Console.log(e + " == "+json.get(e).toString());
                String v = json.getString(e);

                boolean matches = v.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$");

                System.out.println("matches = " + matches);
                String concat = StrUtil.concat(true, v, " ","00:00:01");

                LocalDateTime parse = LocalDateTime.parse(concat, DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS));

                System.out.println("parse = " + parse);
            });
        }
    }
}